package cn.com.ghy.android.widget.chat;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import cn.com.ghy.android.bean.OrderDetailBean;
import cn.com.ghy.android.widget.R;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:订单信息预览
 * @author wuz  @date 2011-5-18 11:35:44
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class OrderInfoPreview extends Activity {

	private TextView labTxtOrderTotalPricePreview = null;
	private ListView listView = null;
	private ImageButton btnBack = null;

	private List<OrderDetailBean> orderDetailAll = null;

	private OrderInfoAdapter orderInfoAdapter = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.order_info_preview);
		findView();
		getSession();
		setToolBar();
		init();
		addListener();
	}

	private void findView() {
		labTxtOrderTotalPricePreview = (TextView) this
				.findViewById(R.id.labTxtOrderTotalPricePreview);
		listView = (ListView) this.findViewById(R.id.listOrderDetailListPreview);
	}

	@SuppressWarnings("unchecked")
	private void getSession() {
		orderDetailAll = (List<OrderDetailBean>) this.getIntent().getSerializableExtra(
				"orderDetailAll");
	}

	private void setToolBar() {
		View appToolBar = findViewById(R.id.appToolBar);
		ImageButton btnList = (ImageButton) appToolBar.findViewById(R.id.btnList);
		btnList.setVisibility(View.GONE);
		btnBack = (ImageButton) appToolBar.findViewById(R.id.btnBack);
	}

	private void init() {
		labTxtOrderTotalPricePreview.setText("0.00");
		for (Iterator<OrderDetailBean> it = orderDetailAll.iterator(); it.hasNext();) {
			OrderDetailBean od = (OrderDetailBean) it.next();
			if ("0".equals(od.getNum()) || "0".equals(od.getTotalprice())) {
				it.remove();
			}
		}

		orderInfoAdapter = new OrderInfoAdapter(this);
		listView.setAdapter(orderInfoAdapter);
		orderInfoAdapter.addAll(orderDetailAll);
	}

	private void addListener() {
		btnBack.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				switch (v.getId()) {
					case R.id.btnBack :
						OrderInfoPreview.this.finish();
						break;
				}
			}
		});
	}

	private class OrderInfoAdapter extends BaseAdapter {

		private Context context = null;
		private List<OrderDetailBean> list = new ArrayList<OrderDetailBean>();

		public OrderInfoAdapter(Context context) {
			this.context = context;
		}

		public int getCount() {
			return list.size();
		}

		public OrderDetailBean getItem(int position) {
			return list.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			String inflater = Context.LAYOUT_INFLATER_SERVICE;
			LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(inflater);
			LinearLayout linearLayout = null;
			if (list.get(position) instanceof OrderDetailBean) {
				linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.order_info_lay, null);
				TextView textView = ((TextView) linearLayout.findViewById(R.id.labTxtOrderInfoSKU));
				textView.setText(list.get(position).getBismt());

				textView = ((TextView) linearLayout.findViewById(R.id.labTxtMatnr));
				textView.setText(list.get(position).getMatnr());

				textView = ((TextView) linearLayout.findViewById(R.id.labTxtMaktx));
				textView.setText(list.get(position).getMaktx());

				textView = ((TextView) linearLayout.findViewById(R.id.labTxtTotalprice));
				textView.setText(list.get(position).getTotalprice() + "元");

				textView = ((TextView) linearLayout.findViewById(R.id.labTxtOrderInfoNum));
				textView.setText(list.get(position).getNum()
						+ list.get(position).getUnitInfo().getUnitname());

				textView = ((TextView) linearLayout.findViewById(R.id.labTxtOrderPrice));
				textView.setText(list.get(position).getOrderprice() + "元");

				textView = ((TextView) linearLayout.findViewById(R.id.labTxtOrderComments));
				textView.setText(list.get(position).getRemark());
			}
			return linearLayout;
		}

		public void addAll(List<OrderDetailBean> list) {
			this.list = list;
			totalPriceCount(list);
			notifyDataSetChanged();
		}

		private void totalPriceCount(List<OrderDetailBean> list) {
			double totalPrice = 0;
			for (OrderDetailBean orderDetail : list) {
				totalPrice += Double.parseDouble(orderDetail.getTotalprice());
			}
			java.text.DecimalFormat df = new java.text.DecimalFormat("######0.00");
			labTxtOrderTotalPricePreview.setText(df.format(totalPrice));
		}
	}
}
